package com.ruoyi.web.controller.inspection;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.config.HeadNum;
import com.ruoyi.common.core.domain.Ajax2Result;
import com.ruoyi.common.utils.BeanUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.GetSessionValueUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.inspection.domain.FzInspectionDian;
import com.ruoyi.inspection.domain.FzInspectionRoute;
import com.ruoyi.inspection.domain.FzInspectionRouteInfo;
import com.ruoyi.inspection.service.IFzInspectionDianService;
import com.ruoyi.inspection.service.IFzInspectionRouteInfoService;
import com.ruoyi.inspection.service.IFzInspectionRouteService;
import com.ruoyi.web.controller.inspection.vo.FzInspectionRouteInfoVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 路线Controller
 * 
 * @author ruoyi
 * @date 2020-03-13
 */
@Controller
@RequestMapping("/inspection/route")
public class FzInspectionRouteController extends BaseController
{
    private String prefix = "inspection/route";

    @Autowired
    private IFzInspectionRouteService fzInspectionRouteService;

    @RequiresPermissions("inspection:route:view")
    @GetMapping()
    public String route()
    {
        return prefix + "/route";
    }

    /**
     * 查询路线列表
     */
    @RequiresPermissions("inspection:route:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FzInspectionRoute fzInspectionRoute)
    {
        startPage();
        fzInspectionRoute.setcId(GetSessionValueUtil.getCommunity());
        List<FzInspectionRoute> list = fzInspectionRouteService.selectFzInspectionRouteList(fzInspectionRoute);
        return getDataTable(list);
    }

    /**
     * 导出路线列表
     */
    @RequiresPermissions("inspection:route:export")
    @Log(title = "路线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FzInspectionRoute fzInspectionRoute)
    {
        List<FzInspectionRoute> list = fzInspectionRouteService.selectFzInspectionRouteList(fzInspectionRoute);
        ExcelUtil<FzInspectionRoute> util = new ExcelUtil<FzInspectionRoute>(FzInspectionRoute.class);
        return util.exportExcel(list, "route");
    }

    /**
     * 新增路线
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存路线
     */
    @RequiresPermissions("inspection:route:add")
    @Log(title = "路线", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FzInspectionRoute fzInspectionRoute)
    {
        fzInspectionRoute.setRouteId(HeadNum.ROUTE_HEAD+ DateUtils.getTimeMillis());
        fzInspectionRoute.setcId(GetSessionValueUtil.getCommunity());
        fzInspectionRoute.setCreateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzInspectionRouteService.insertFzInspectionRoute(fzInspectionRoute));
    }

    /**
     * 修改路线
     */
    @GetMapping("/edit/{routeId}")
    public String edit(@PathVariable("routeId") String routeId, ModelMap mmap)
    {
        FzInspectionRoute fzInspectionRoute = fzInspectionRouteService.selectFzInspectionRouteById(routeId);
        mmap.put("fzInspectionRoute", fzInspectionRoute);
        return prefix + "/edit";
    }

    /**
     * 修改保存路线
     */
    @RequiresPermissions("inspection:route:edit")
    @Log(title = "路线", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FzInspectionRoute fzInspectionRoute)
    {
        fzInspectionRoute.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzInspectionRouteService.updateFzInspectionRoute(fzInspectionRoute));
    }

    /**
     * 删除路线
     */
    @RequiresPermissions("inspection:route:remove")
    @Log(title = "路线", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {

        return toAjax(fzInspectionRouteService.deleteFzInspectionRouteByIds(ids));
    }


    @GetMapping("/dian")
    public String editDian(ModelMap map, @RequestParam(name="id",required = false ) String id)
    {
        map.put("routeId",id);
        return prefix + "/dian";
    }

    @Autowired
    private IFzInspectionRouteInfoService fzInspectionRouteInfoService;

    @RequiresPermissions("inspection:info:view")
    @GetMapping("/info")
    public String info(ModelMap map, @RequestParam("routeId")String routeId)
    {
        map.put("routeId",routeId);
        return  "inspection/info/info";
    }

    @GetMapping("/look")
    public String look(ModelMap map, @RequestParam("routeId")String routeId)
    {
        map.put("routeId",routeId);
        return  "inspection/route/xian";
    }

    @Autowired
    private IFzInspectionDianService fzInspectionDianService;
    @PostMapping( "/look")
    @ResponseBody
    public Ajax2Result look(@RequestParam("routeId")String routeId)
    {
        FzInspectionRouteInfo fzInspectionRouteInfo=new FzInspectionRouteInfo();
        fzInspectionRouteInfo.setRouteId(routeId);

        List<FzInspectionRouteInfo> list = fzInspectionRouteInfoService.selectFzInspectionRouteInfoList(fzInspectionRouteInfo);
        List<FzInspectionRouteInfoVo> fzInspectionRouteInfoVos=new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)) {
            List<String> inspectionRouteInfoId = list.stream().map(FzInspectionRouteInfo::getInspectionId).collect(Collectors.toList());
            List<FzInspectionDian> fzInspectionDians = fzInspectionDianService.selectFzInspectionDianListById(inspectionRouteInfoId);
            if (!CollectionUtils.isEmpty(fzInspectionDians)) {
                Map<String,FzInspectionDian> fzInspectionDianMap=fzInspectionDians.stream().collect(Collectors.toMap(FzInspectionDian::getInspectionId, FzInspectionDian->FzInspectionDian));
                for(FzInspectionRouteInfo fzInspectionRouteInfo1 : list){
                    FzInspectionRouteInfoVo fzInspectionRouteInfoVo=new FzInspectionRouteInfoVo();
                    BeanUtil.copyProperties(fzInspectionRouteInfo1,fzInspectionRouteInfoVo);
                    if(fzInspectionDianMap.containsKey(fzInspectionRouteInfoVo.getInspectionId())){
                        fzInspectionRouteInfoVo.setName(fzInspectionDianMap.get(fzInspectionRouteInfoVo.getInspectionId()).getName());
                        fzInspectionRouteInfoVo.setAlong(fzInspectionDianMap.get(fzInspectionRouteInfoVo.getInspectionId()).getAlong());
                        fzInspectionRouteInfoVo.setLatitude(fzInspectionDianMap.get(fzInspectionRouteInfoVo.getInspectionId()).getLatitude());
                        fzInspectionRouteInfoVos.add(fzInspectionRouteInfoVo);
                    }
                }
            }
        }
        fzInspectionRouteInfoVos.stream().sorted(Comparator.comparing(FzInspectionRouteInfoVo::getSort));
        return Ajax2Result.success(fzInspectionRouteInfoVos);
}


}
